#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#author:         rex
#blog:           http://iregex.org
#created:        2010-06-26 18:59

import re
from twitter import Twitter
from snip import AddSnip
import setting

class Disp:
    def __init__(self, string, client):
        m=re.match(r"^\s*(\w+)\s*((?s).*)", string)
        if m:
            self.cmd=m.group(1)
            self.msg=m.group(2)
            self.clt=client
        else:
            pass

    def do(self):
        if self.cmd=='tt':
            try:
                twclient = Twitter(setting.user,setting.pwd) 
                twclient.update_status(self.msg)
                self.clt.reply("tt msg sent!") 
            except:
                self.clt.reply("tt msg update failed!")
        elif self.cmd=='tf': 
            try:
                twclient = Twitter(setting.user,setting.pwd) 
                msg=twclient.get_friends_timeline()
                res=""
                for m in msg:
                    res+=m['user']['name']+":\t"+m['text']+"\n\n" 

                self.clt.reply(res) 
            except:
                self.clt.reply("get friends msg failed!")

        elif self.cmd=='h' or self.cmd=='help':
            self.clt.reply('''Usage:
==============================
##t: twitter related commands
tt: update twitter message; 
tf: get friends messages
    tfo:  older messages
    tfn:  newer messages
tr: get replies
==============================
##c: code snippets
cc Subject\\n#tags\\nBody:  add a snippet 

==============================

e or echo: Echo back the message.
h or help: to print this message.
''')
        elif self.cmd=='cc':

            try:
                snip= AddSnip(self.msg, self.clt) 
                snip.Do()
            except:
                self.clt.reply("add snip error when dispatching!") 

        elif self.cmd=='e' or self.cmd=='echo':
            self.clt.reply(self.msg)
        else:
            self.clt.reply("Unknow command!\nPlease use 'h' or 'help' for help.")
            
                        
        
